/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package br.zeos.bean;

import br.zeos.ZConnect;
import br.zeos.ZDataSource;
import br.zeos.ZSql;
import br.zeos.ZTable;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.ArrayList;
import java.util.HashMap;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;

/**
 *
 * @author virtual
 */
public class ZComboBox extends JComboBox<String> implements ActionListener {

    private ZDataSource dataSource = null; //MALDITO, DA PROXIMA VEZ COMEÇA VARIÁVEL COM LETRA MINÚSCULA
    private String FieldName = ""; //DEPOIS EU REFATORO O RESTO DAS VARIÁVEIS...
    private String Relation_TableName = "";
    private String Relation_Field = "";
    private String Relation_FieldShow = "";

    public ZDataSource getdataSource() {
        return dataSource;
    }

    public void setDataSource(ZDataSource dataSource) {
        this.dataSource = dataSource;
        this.dataSource.addUpdateListener(this);
        this.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent ie) {
                UpdateData();
            }
        });
    }

    public void setFillField(String campo) {
        ZConnect conexao = this.dataSource.getSql().getTabela().getConnection();
                
        DefaultComboBoxModel<String> modelo = (DefaultComboBoxModel<String>) this.getModel();
        modelo.removeAllElements();
        for (HashMap<String, String> e : this.dataSource.getSql().Select()) {
            modelo.addElement(e.get(campo.toUpperCase()));
        }
        this.setModel(modelo);
    }

    public void UpdateData() {
        //Atualiza o dataSource        
        if (this.getModel().getSize() < 1) {
            System.out.println("Retornei para as colinas com medo do pontos em branco destruidores de tela");
            return;
        }
        if(this.Relation_TableName.isEmpty()){
            return;
        }
        try {
            HashMap<String, String> linha = this.dataSource.getLine();
            String value = this.getSelectedItem().toString();
            ZConnect conexao = this.dataSource.getSql().getTabela().getConnection();
            ZTable tabelaRelacionada = new ZTable(conexao, this.Relation_TableName);
            ZSql sql = new ZSql(tabelaRelacionada);
            sql.putParameter(":VALUE", value, ZSql.Tipos.String);
            value = sql.Select(this.Relation_FieldShow + "= :VALUE", -1, -1).get(0).get(this.Relation_Field);
            linha.put(this.FieldName.toUpperCase(), value);
            this.dataSource.setLinha(linha);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public String getfieldName() {
        return FieldName;
    }

    public void setFieldName(String fieldName) {
        this.FieldName = fieldName.toUpperCase();
    }

    public void setRelation(String TableName, String Field, String FieldShow) {
        this.Relation_TableName = TableName.toUpperCase();
        this.Relation_Field = Field.toUpperCase(); //PRA QUE USAR ISTO?
        this.Relation_FieldShow = FieldShow.toUpperCase();
        ZConnect conexao = this.dataSource.getSql().getTabela().getConnection();
        ZTable tabela = new ZTable(conexao, this.Relation_TableName);
        ZSql sql = new ZSql(tabela);
        DefaultComboBoxModel<String> modelo = (DefaultComboBoxModel<String>) this.getModel();
        modelo.removeAllElements();
        for (HashMap<String, String> e : sql.Select()) {
            modelo.addElement(e.get(this.Relation_FieldShow));
        }
        this.setModel(modelo);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        //set Value
        String id = "";
        HashMap<String, String> linha = this.dataSource.getLine(); //.get(this.fieldName.toUpperCase());
        id = linha.get(this.FieldName.toUpperCase());//aqui
        ZConnect conexao = this.dataSource.getSql().getTabela().getConnection();
        ZTable tabela = new ZTable(conexao, this.Relation_TableName);
        ZSql sql = new ZSql(tabela);
        String value = "";
        sql.putParameter(":CACHORROQUENTE", id, ZSql.Tipos.String);
        value = sql.Select(this.Relation_Field + "= :CACHORROQUENTE", -1, -1).get(0).get(this.Relation_FieldShow);
        this.setSelectedItem(value);
    }

}
